//
//  RadarView.m
//  BrushFace
//
//  Created by 吕海瑞 on 15/12/26.
//  Copyright © 2015年 Zzz. All rights reserved.
//
#import "popVIew.h"
#import "RadarView.h"
#import "RadarRemindView.h"

#import "EstimateLable.h"
#import <AudioToolbox/AudioToolbox.h>
@interface RadarView ()<XHRadarViewDelegate,XHRadarViewDataSource>
@property (nonatomic, strong) XHRadarView *radarView;

@property (nonatomic, strong) RadarRemindView *ramindView;
@property (nonatomic, strong) EstimateLable *estimateLable;
@property (nonatomic,strong)NSMutableArray *dataArray;//存放要显示的点
@end
@implementation RadarView

- (void)radarViewStartScan
{
    [self.radarView scan];
    [self startUpdatingRadar];
}
- (void)radarViewStopScan
{
    [self.radarView stop];
}


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addXHRadarView];
            }
    return self;
}

-(void)addXHRadarView
{
    XHRadarView *radarView = [[XHRadarView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    radarView.dataSource = self;
    radarView.delegate = self;
    radarView.radius = 230;
    radarView.backgroundColor = [UIColor clearColor];
    [self addSubview:radarView];
    _radarView = radarView;
    
    [self addCenterView];
    
    
    _ramindView = [[RadarRemindView alloc]initWithFrame: CGRectMake(0, 0, SCREEN_WIDTH, 55)];
    
    _ramindView.hidden = YES;
    //_ramindView.remainsecond = 172798;
    [_radarView addSubview:_ramindView];
    
    
    _estimateLable = [[EstimateLable alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 80-64, SCREEN_WIDTH, 30)];
    [_ramindView addSubview:_estimateLable];
    _pointsArray = @[
                     @[@6, @90],
                     @[@-140, @108],
                     @[@-83, @98],
                     @[@-25, @142],
                     @[@60, @111],
                     @[@-111, @96],
                     @[@150, @145],
                     @[@25, @144],
                     @[@-55, @110],
                     @[@95, @109],
                     @[@170, @180],
                     @[@125, @112],
                     @[@-150, @165],
                     ];
    
    
    //[self.radarView scan];
    
}

#pragma mark - Custom Methods
- (void)startUpdatingRadar {
    typeof(self) __weak weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //        weakSelf.radarView.labelText = [NSString stringWithFormat:@"搜索已完成，共找到%lu个朋友", (unsigned long)weakSelf.dataArray.count];
        [weakSelf.radarView show];
        
    });
}
- (void)addHopeidToView:(NSString *)hopeid{
    [_alreadyBorrowView setViewHopeIDWithHopeid:hopeid];
}
-(void)addCenterView
{
    AlreadyBorrowView *alreadyBorrowView = [[AlreadyBorrowView alloc]initWithFrame:CGRectMake(_radarView.center.x-39, _radarView.center.y-39, 78, 78)];
    alreadyBorrowView.hopeid = self.hopeid;
    alreadyBorrowView.layer.cornerRadius = 78/2;
    [_radarView addSubview:alreadyBorrowView];
    [_radarView bringSubviewToFront:alreadyBorrowView];
    self.alreadyBorrowView = alreadyBorrowView;
}
#pragma mark - XHRadarViewDataSource
- (NSInteger)numberOfSectionsInRadarView:(XHRadarView *)radarView
{
    return 4;
}
- (NSInteger)numberOfPointsInRadarView:(XHRadarView *)radarView
{
   
    return [self.dataArray count];
}

- (UIView *)radarView:(XHRadarView *)radarView viewForIndex:(NSUInteger)index {
    popVIew *pointView = [[popVIew alloc] initWithFrame:CGRectMake(0, 0, 120, 45)];
    NSDictionary *dic = [_dataArray objectAtIndex:index];
    if ([dic[@"level"] isNotEmpty])
    {
        if ([[NSString stringWithFormat:@"%@",dic[@"level"]]
             intValue] == 1 )
        {
           
            pointView.whoStr = @"我的朋友";
        }
        else if ([[NSString stringWithFormat:@"%@",dic[@"level"]]
                  intValue] == 2||[[NSString stringWithFormat:@"%@",dic[@"level"]]
                                   intValue] == 0)
        {
            pointView.whoStr = @"朋友的朋友";
        }
    }
    if ([dic[@"amount" ] isNotEmpty])
    {
        pointView.num    = [NSString stringWithFormat:@"%@",dic[@"amount"]];
    }
    return pointView;
}
- (CGPoint)radarView:(XHRadarView *)radarView positionForIndex:(NSUInteger)index
{
    
    //系统声音
    AudioServicesPlaySystemSound(1007);

    //震动
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    NSArray *point = [self.pointsArray objectAtIndex:index];
    return CGPointMake([point[0] floatValue], [point[1] floatValue]);
}

#pragma mark - XHRadarViewDelegate

- (void)radarView:(XHRadarView *)radarView didSelectItemAtIndex:(NSUInteger)index {
    // NSLog(@"didSelectItemAtIndex:%lu", (unsigned long)index);
    
}

-(void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    
    
    if (dataDic.count>0)
    {
        if ([_dataDic[@"investamount"] isNotEmpty])
        {
            //借到的总数
            
            _alreadyBorrowView.num = [NSString stringWithFormat:@"%@",_dataDic[@"investamount"]];
            
        }
        if ([_dataDic[@"counthopemessage"] isNotEmpty])
        {
            _ramindView.hidden = NO;
            _ramindView.friendLable.text = [NSString stringWithFormat:@"已经通知%@位好友, ",_dataDic[@"counthopemessage"]];
            
            
        }
        if ([_dataDic[@"remainsecond"] isNotEmpty])
        {
            _ramindView.remainsecond = [[NSString stringWithFormat:@"%@",_dataDic[@"remainsecond"]] integerValue];
        }
        
        if ([_dataDic[@"expiredate"] isNotEmpty])
        {
           // _dataDic[@"expiredate"]
            _estimateLable.expiredate =[NSString stringWithFormat:@"%@",_dataDic[@"remainsecond"]];
        }
        
        if ([_dataDic[@"data"] isNotEmpty])
        {
            NSArray *array = _dataDic[@"data"];
            if (array.count>0)
            {
                
                
                if (!_dataArray) {
                    _dataArray = [NSMutableArray array];
                }
                else
                {
                    [_dataArray removeAllObjects];
                }
                for (int i = 0; i<array.count;i++)
                {
                    if (i<10)
                    {
                        NSDictionary *pointDic = [[NSDictionary alloc]init];
                        pointDic = array[i];
                        [_dataArray addObject:pointDic];
                    }
                    else
                    {
                        break;
                    }
                }
                [self startUpdatingRadar];
                
            }
        }
    }

    
}
-(void)stopTimer
{
    [_ramindView timerStop];
    [_estimateLable timerStop];
}
@end
